import router from "./router"
import store from "./store"

//路由白名单
const whiteList: string[] = ["login", "register", "home", "classify"]; //路由每个节点name的命名都一定要规范化且唯一
router.beforeEach((to, from, next) => {
    if (store.getters.token) {
        if (to.name === "login") {
            next({ name: "basic" });
        }
        const redirect = decodeURIComponent(from.query.redirect as string || to.path);
        if(to.path == redirect){
            next({...to, replace: true});
        }else{
            next({ path: redirect });
        }
    } else {
        if (whiteList.includes(to.name as string)) {
            next();
        } else {
            next({ name: "login" });
        }
    }
})